import Vue from 'vue'
import VueRouter from 'vue-router'
import layout from '../components/layout.vue'
import login from '../components/login.vue'
import selfDetail from '../components/selfDetail.vue'
import errorPage from '../components/errorPage.vue'

// import tzgg from '../components/content/tzgg.vue'
// import hydt from '../components/content/hydt.vue'
// import zhyy from '../components/content/zhyy.vue'
// import fxkh from '../components/content/fxkh.vue'
// import zlxz from '../components/content/zlxz.vue'
// import grrc from '../components/content/grrc.vue'
// import dzyj from '../components/content/dzyj/index.vue'
// import dbsx from '../components/content/dbsx.vue'
// import mkdz from '../components/content/mkdz.vue'
// import txl from '../components/content/txl.vue'

// const tzgg = () => import('../components/content/tzgg.vue')
const tzgg = resolve => require(['../components/content/tzgg/index.vue'], resolve)
const noticeDetail = resolve => require(['../components/content/tzgg/noticeDetail.vue'], resolve)
const hydt = resolve => require(['../components/content/hydt.vue'], resolve)
const zhyy = resolve => require(['../components/content/zhyy.vue'], resolve)
const fxkh = resolve => require(['../components/content/fxkh/fxkh.vue'], resolve)
const zlxz = resolve => require(['../components/content/zlxz.vue'], resolve)
const grrc = resolve => require(['../components/content/grrc.vue'], resolve)
// const dzyj = resolve => require(['../components/content/dzyj/index.vue'], resolve)
// const dbsx = resolve => require(['../components/content/dbsx.vue'], resolve)
const mkdz = resolve => require(['../components/content/mkdz.vue'], resolve)
const txl = resolve => require(['../components/content/txl.vue'], resolve)


import config from '../scripts/config.js'
import _ from 'lodash'

Vue.use(VueRouter)

const mkstr = localStorage.mk;
var mk = [], redirectRoute = 'tzgg';
if (mkstr && mkstr != 'null') {
    mk = mkstr.split(',');
    redirectRoute = mk[0]
}

const router = new VueRouter({
    mode: 'history',
    routes: [
        { path: '/', redirect: { name: redirectRoute } },
        { path: '/login', name: 'login', component: login },
        { path: '/selfDetail', component: selfDetail },
        { path: '/noticeDetail/:id', component: noticeDetail },
        {
            path: '/content', component: layout, redirect: { name: 'tzgg' },
            children: [{
                path: 'tzgg', name: 'tzgg', component: tzgg
            },
            {
                path: 'hydt', name: 'hydt', component: hydt
            },
            {
                path: 'zhyy', name: 'zhyy', component: zhyy
            },
            {
                path: 'fxkh', name: 'fxkh', component: fxkh
            },
            {
                path: 'zlxz', name: 'zlxz', component: zlxz
            },
            {
                path: 'grrc', name: 'grrc', component: grrc
            },
            // {
            //     path: 'dzyj', name: 'dzyj', component: dzyj
            // },
            // {
            //     path: 'dbsx', name: 'dbsx', component: dbsx
            // },
            {
                path: 'txl', name: 'txl', component: txl
            },
            {
                path: 'mkdz', name: 'mkdz', component: mkdz
            }
            ]
        },
        { path: '*', component: errorPage }
    ]
})

router.beforeEach((to, from, next) => {
    if (config && config.http) {
        config.http("GET_HEAD_MENU").then(data => {
            let curMenu = _.filter(data, item => {
                return to.path.indexOf(item.route) > -1
            })[0];
            if (curMenu && curMenu.needLogin) {
                if (localStorage.isLogin) {
                    next()
                } else {
                    config.store.commit('SET_ROUTE_TARGET', to.fullPath);
                    localStorage.clear()
                    router.push('/login')
                }
            } else {
                next()
            }
        })
    } else {
        next()
    }
})

export default router